package apt.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import apt.bean.AdvertisingBean;
import apt.bean.RoleBean;
import apt.common.HibernateUtil;

import com.sun.xml.internal.ws.handler.HandlerException;

public class AdvertistingDao {
	private Session session;
	private Transaction transaction;
	private List<AdvertisingBean> listAdv;

	public List<AdvertisingBean> getAdvertisting() {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			String sql = "FROM AdvertisingBean";
			Query query = session.createQuery(sql);
			listAdv = query.list();
		} catch (HandlerException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return listAdv;
	}

	public boolean addAdvertisting(AdvertisingBean adv) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			transaction = session.beginTransaction();
			session.save(adv);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	public boolean updateRole(AdvertisingBean adv) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			transaction = session.beginTransaction();
			session.update(adv);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		} finally {
			session.close();
		}
		return true;
	}
}
